﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TeamOPSchoolCup.Domain.POCO;
using TeamOPSchoolCup.Domain.DAL;

namespace TeamOPSchoolCup.Domain.Services
{
  public  class EventService
  {
    public static bool AddEvent(Event pEvent)
    {
      bool e;
      using (EFDbContext db = new EFDbContext())
      {
        e = AddEvent(db, pEvent);
      }
      return e;
    }

    public static bool AddEvent(EFDbContext db, Event pEvent)
    {
      db.Events.Add(pEvent);
      return (db.SaveChanges() > 0);
    }
    public static Event GetEvent(string name)
    {
      Event r;
      using (EFDbContext db = new EFDbContext())
      {
        r = GetEvent(db, name);
      }
      return r;
    }

    public static Event GetEvent(EFDbContext db, string name)
    {
      return db.Events.Where(x => x.Name.Equals(name)).FirstOrDefault();
    }

    public static List<Event> GetEvents()
    {
      List<Event> events;
      using(EFDbContext db = new EFDbContext()){
        events = GetEvents(db);
      }

      return events;
    }

    public static List<Event> GetEvents(EFDbContext db)
    {
      return db.Events.ToList<Event>();
    }
    public static List<Event> GetEventsOfSport(Sport sport) 
    {
      List<Event> events;
      using (EFDbContext db = new EFDbContext())
      {
        events = GetEventsOfSport(db,sport);
      }

      return events;
    }
    public static List<Event> GetEventsOfSport(EFDbContext db,Sport sport)
    {
      return db.Events.Where(e=> e.Sport == sport).ToList<Event>();
    }
  }
}
